import { createSlice,createAsyncThunk } from '@reduxjs/toolkit';
import { findById } from '../../services/student';

// 创建中间件函数，其中回调函数返回的数据会交给reducer
export const fetchStudent = createAsyncThunk('student/fetchStudent',async id => {
    let student = await findById(id);
    return student;
});

const studentSlice = createSlice({
    name:"student",
    initialState:{
        updateVisible:false,
        student:{}
    },
    reducers:{
        setUpdateVisible(state,action){
            state.updateVisible = action.payload;
        },
        // setStudent(state,action){
        //     state.student = action.payload;
        // }
    },
    extraReducers(build){
        build.addCase(fetchStudent.fulfilled,(state,action) => {
            state.student = action.payload;
        });
    }
});
export const {setUpdateVisible} = studentSlice.actions;
export default studentSlice.reducer;